package com.ziyucoding.springboot.labs.lab4.concurrentdemo.example.singleton;

import com.ziyucoding.springboot.labs.lab4.concurrentdemo.annoations.Recommend;
import com.ziyucoding.springboot.labs.lab4.concurrentdemo.annoations.ThreadSafe;

/**
 * 单例模式
 * 枚举模式：最安全
 *
 * @author ziyucoding
 * @date 2022-10-26
 **/
@ThreadSafe
@Recommend
public class SingletonExample7 {

    /**
     * 单例
     */
    private static SingletonExample7 instance = null;

    private SingletonExample7() {

    }

    /**
     * 获取实例
     *
     * @param
     * @return SingletonExample7
     **/
    public static SingletonExample7 getInstance() {
        return Singleton.INSTANCE.getSingleton();
    }

    /**
     * 枚举
     **/
    private enum Singleton {
        INSTANCE;

        private SingletonExample7 singleton;

        /**
         * JVM保证这个方法绝对只调用一次
         *
         * @param
         * @return
         **/
        Singleton() {
            singleton = new SingletonExample7();
        }

        public SingletonExample7 getSingleton() {
            return singleton;
        }

    }

    public static void main(String[] args) {
        System.out.println(getInstance().hashCode());
        System.out.println(getInstance().hashCode());
    }
}
